package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.WorkPatentWinners;
import com.ruoyi.system.mapper.WorkPatentMapper;
import com.ruoyi.system.domain.WorkPatent;
import com.ruoyi.system.service.IWorkPatentService;

/**
 * 著作
Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-11
 */
@Service
public class WorkPatentServiceImpl implements IWorkPatentService 
{
    @Autowired
    private WorkPatentMapper workPatentMapper;

    /**
     * 查询著作

     * 
     * @param id 著作
主键
     * @return 著作

     */
    @Override
    public WorkPatent selectWorkPatentById(Long id)
    {
        return workPatentMapper.selectWorkPatentById(id);
    }

    /**
     * 查询著作
列表
     * 
     * @param workPatent 著作

     * @return 著作

     */
    @Override
    public List<WorkPatent> selectWorkPatentList(WorkPatent workPatent)
    {
        return workPatentMapper.selectWorkPatentList(workPatent);
    }

    /**
     * 新增著作

     * 
     * @param workPatent 著作

     * @return 结果
     */
    @Transactional
    @Override
    public int insertWorkPatent(WorkPatent workPatent)
    {
        int rows = workPatentMapper.insertWorkPatent(workPatent);
        insertWorkPatentWinners(workPatent);
        return rows;
    }

    /**
     * 修改著作

     * 
     * @param workPatent 著作

     * @return 结果
     */
    @Transactional
    @Override
    public int updateWorkPatent(WorkPatent workPatent)
    {
        workPatentMapper.deleteWorkPatentWinnersByWorkPatentId(workPatent.getId());
        insertWorkPatentWinners(workPatent);
        return workPatentMapper.updateWorkPatent(workPatent);
    }

    /**
     * 批量删除著作

     * 
     * @param ids 需要删除的著作
主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWorkPatentByIds(Long[] ids)
    {
        workPatentMapper.deleteWorkPatentWinnersByWorkPatentIds(ids);
        return workPatentMapper.deleteWorkPatentByIds(ids);
    }

    /**
     * 删除著作
信息
     * 
     * @param id 著作
主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWorkPatentById(Long id)
    {
        workPatentMapper.deleteWorkPatentWinnersByWorkPatentId(id);
        return workPatentMapper.deleteWorkPatentById(id);
    }

    /**
     * 新增著作完成人
信息
     * 
     * @param workPatent 著作
对象
     */
    public void insertWorkPatentWinners(WorkPatent workPatent)
    {
        List<WorkPatentWinners> workPatentWinnersList = workPatent.getWorkPatentWinnersList();
        Long id = workPatent.getId();
        if (StringUtils.isNotNull(workPatentWinnersList))
        {
            List<WorkPatentWinners> list = new ArrayList<WorkPatentWinners>();
            for (WorkPatentWinners workPatentWinners : workPatentWinnersList)
            {
                workPatentWinners.setWorkPatentId(id);
                list.add(workPatentWinners);
            }
            if (list.size() > 0)
            {
                workPatentMapper.batchWorkPatentWinners(list);
            }
        }
    }
}
